package com.doubi.service;//package com.gaoguo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.constants.SystemConstants;
import com.doubi.mapper.AreaMapper;
import com.doubi.model.sys.TArea;
import com.doubi.model.sys.TCity;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import com.doubi.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能说明：区域 服务层实现
 * @author MrDoubi
 * @date 2019-05-30 10:22:49
 */
@Service("areaService")
public class AreaService extends ServiceImpl<AreaMapper, TArea> {
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private CityService cityService;



    /**
     * 功能说明：查询区域分页信息
     * @param page page
     * @param area bean
     * @return page
     */
    public PageVo<TArea> getPage(PageVo<TArea> page, TArea area) {
        QueryWrapper<TArea> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(area.getAreaName())){
            wrapper.like("area_name",area.getAreaName());
        }
        if(DataUtil.isNotEmpty(area.getAreaCode())){
            wrapper.like("area_code",area.getAreaCode());
        }
        if(DataUtil.isNotEmpty(area.getCityOid())){
            wrapper.like("city_oid",area.getCityOid());
        }
        if(DataUtil.isNotEmpty(area.getStatus())){
            wrapper.eq("status",area.getStatus());
        }
        wrapper.orderByAsc("sort_num");
        areaMapper.selectPage(page,wrapper);
        return page;
    }


    /**
     * 功能说明：查询区域列表信息
     * @param area bean
     * @return list
     */
    public List<TArea> getList(TArea area) {
        QueryWrapper<TArea> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(area.getAreaName())){
            wrapper.like("area_name",area.getAreaName());
        }
        if(DataUtil.isNotEmpty(area.getAreaCode())){
            wrapper.like("area_code",area.getAreaCode());
        }
        if(DataUtil.isNotEmpty(area.getCityOid())){
            wrapper.like("city_oid",area.getCityOid());
        }
        if(DataUtil.isNotEmpty(area.getStatus())){
            wrapper.eq("status",area.getStatus());
        }
        wrapper.orderByAsc("sort_num");
        return areaMapper.selectList(wrapper);
    }

    /**
     * 功能说明：区域 新增、修改
     * @param area bean
     * @return AppModel
     */
    public AppModel saveOrUpdateBean(TArea area) {
        TCity city = cityService.getById(area.getCityOid());
        if(DataUtil.isEmpty(city)){
            return new AppModel(-1,"市区不存在！");
        }else {
            area.setCityCode(city.getCityCode());
        }
        //修改操作
        if (DataUtil.isNotEmpty(area.getAreaOid())) {
            TArea bean = areaMapper.selectById(area.getAreaOid());
            if (DataUtil.isEmpty(bean)) {
                return new AppModel(-1, "参数错误！");
            }
            //进行 数据的拷贝
            bean.setAreaCode(area.getAreaCode());
            bean.setAreaName(area.getAreaName());
            bean.setSortNum(area.getSortNum());
            bean.setStatus(area.getStatus());
            areaMapper.updateById(bean);
        } else {
            //进行初始化操作
            areaMapper.insert(area);
        }
        return new AppModel(0,"操作成功！");
    }
    /**
     * 功能说明：区域 删除
     * @param area bean
     * @return AppModel
     */
    public AppModel delBean(TArea area) {
        area = areaMapper.selectById(area.getAreaOid());
        if (DataUtil.isEmpty(area)) {
            return new AppModel(-1, "参数错误！");
        } else {
            areaMapper.deleteById(area.getAreaOid());
            return new AppModel(0, "删除成功！");
        }
    }

    /**
     * 功能说明：市区 更新状态
     * @param area bean
     * @return AppModel
     */
    public AppModel updateStatus(TArea area) {
        area = areaMapper.selectById(area.getAreaOid());
        if (DataUtil.isEmpty(area)) {
            return new AppModel(-1, "参数错误！");
        } else {
            if(area.getStatus().equals(SystemConstants.STATUS.STATUS_1)){
                area.setStatus(SystemConstants.STATUS.STATUS_0);
            }else {
                area.setStatus(SystemConstants.STATUS.STATUS_1);
            }
            areaMapper.updateById(area);
            return new AppModel(0, "修改成功！");
        }
    }
}